package main.daydayup.day0727;

/**
 * 字符串的旋转
 * @author: kejl
 * @create: 2020-07-27 21:28
 */
public class ReverseString {
    public static void main(String[] args) {
        char[] tochar = new char[]{'H', 'E', 'L', 'L', 'O'};
        reverseString2(tochar);
        System.out.println(tochar);
    }

    /**
     * 我使用的是二分法
     *
     * @param s
     */
    public static void reverseString(char[] s) {
        if (s.length == 0) {

        } else {
            for (int i = 0; i <= s.length / 2 - 1; i++) {
                char temp = s[i];
                s[i] = s[s.length - 1 - i];
                s[s.length - 1 - i] = temp;

            }

        }

    }

    /**
     * 使用双指针
     *
     * @param s
     */
    public static void reverseString2(char[] s) {
        int i = 0, j = s.length - 1;
        while (i != j && j != (i - 1)) {
            char temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            i++;
            j--;
        }


    }
}
